package com.tianqu.tms.www.user.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.User;
import com.tianqu.tms.entity.UserClass;
import com.tianqu.tms.www.user.actionform.UserInputActionForm;
import com.tianqu.tms.www.user.viewhelper.UserTopViewHelper;

/**
 * 用户添加Action
 * 
 * @author tianshifu
 *
 */

public class UserCreationConfirmAction extends TmsAction {
		
	private Logger logger = Logger.getLogger(UserCreationConfirmAction.class);
	
	private static final String USERINFO = "userInfo";
	
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		
			logger.debug(this.getClass().getName() + "#execute was called.");
		
			UserInputActionForm inputForm = (UserInputActionForm)form;
			
			UserTopViewHelper view = new UserTopViewHelper();
			
			view = setUserInfoToViewHelper(inputForm,view);
						
			uc.setAttribute(USERINFO, view);//用户提交页获取数据
			
			User user = new User();
			
			//用户唯一性验证
			if(user.findUserInfoByUserId(inputForm.getUserId(),0)==false)
			{
				setErrorsToRequest(request, TmsConstants.IC001 ,null);
				return mapping.getInputForward();				
			}
			logger.debug(this.getClass().getName() + "#doAction was completed.");
			request.setAttribute(TmsConstants.VIEWHELP, view);
			return mapping.findForward(TmsConstants.SUCCESS);
	}
	
	private UserTopViewHelper setUserInfoToViewHelper(UserInputActionForm inputForm,UserTopViewHelper view) throws TmsDaoException{
		
		view.setUserId(inputForm.getUserId());
		view.setUserName(inputForm.getUserName());
		view.setPassword(inputForm.getPassword());
		view.setPassword_cfm(inputForm.getPassword_cfm());
		UserClass code = UserClass.getInstance();
		String uType = code.getUserClassNameByUserClass(Integer.parseInt(inputForm.getUserType()));
		view.setUserType(uType);
		
		return view;
	}

}
